package doantotnghiep.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.DivisionRoomBusiness;
import doantotnghiep.model.DivisionRoom;
import doantotnghiep.modelDTO.DivisionRoomDTO;

@Controller
public class DivisionRoomController {
	
	@ModelAttribute("DivisionRoomDTO")
	public DivisionRoomDTO getDivisionRoomObject() {
		return new DivisionRoomDTO();
	}
	private DivisionRoom divisionRoom;
	
	private DivisionRoomDTO divisionRoomDTO;
	
	@Autowired
	private DivisionRoomBusiness divisionRoomBusiness;

	@RequestMapping(value = "/addRoom", method=RequestMethod.GET)
	public ModelAndView addRoom(){
		return new ModelAndView("addRoom","",null);
	}
	
	@RequestMapping(value = "/addRoom", method = RequestMethod.POST)
	public String addRoom(HttpServletRequest request, ModelMap model,
			@ModelAttribute("DivisionRoomDTO") DivisionRoomDTO divisionRoomDTO){
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
		
			e.printStackTrace();
		}
		System.out.println(request.getCharacterEncoding());
		if(divisionRoomDTO != null){
			DivisionRoom divisionRooma = new DivisionRoom();
			divisionRooma.setRoomName(divisionRoomDTO.getRoomName());
			this.divisionRoomBusiness.addRoom(divisionRooma);
			return "redirect:/viewRoom";	
		}
		return "addRoom";
	}
	
	@RequestMapping(value = "/editRoom", method=RequestMethod.GET)
	public ModelAndView editRoom(HttpServletRequest request, ModelMap model,int id){
		divisionRoom =divisionRoomBusiness.getRoomByID(id);
		divisionRoomDTO = new DivisionRoomDTO();
		
		divisionRoomDTO.setId(divisionRoom.getId());
		divisionRoomDTO.setRoomName(divisionRoom.getRoomName());
		return new ModelAndView("updateRoom", "divisionRoomDTO", divisionRoomDTO);

	}
	
	@RequestMapping(value = "/editRoom", method=RequestMethod.POST)
	public String editRoom( HttpServletRequest request, ModelMap model,
			@ModelAttribute("DivisionRoomDTO") DivisionRoomDTO divisionRoomDTO){
		if(divisionRoomDTO!=null){
			divisionRoom = new DivisionRoom();
			
			divisionRoom.setId(divisionRoomDTO.getId());
			divisionRoom.setRoomName(divisionRoomDTO.getRoomName());
			
			this.divisionRoomBusiness.saveChanged(divisionRoom);
		}
		return "redirect:/viewRoom";
	}
	
	@RequestMapping(value = "/deleteRoom", method = RequestMethod.GET)
	public String deleteRoom(HttpServletRequest request, ModelMap model,
			  int id){
		divisionRoomBusiness.deleteRoom(id);
		return "redirect:/viewRoom";
	}
	
	@RequestMapping(value = "/viewRoom", method = RequestMethod.GET)	
	public ModelAndView listRoom(){
		List<DivisionRoom> divisionRooms;
		List<DivisionRoomDTO>  divisionRoomDTOs = new ArrayList<DivisionRoomDTO>();
		divisionRooms = divisionRoomBusiness.getListRoom();
		if(divisionRooms!=null){
			for (DivisionRoom c : divisionRooms){
				divisionRoomDTO = new DivisionRoomDTO();
				divisionRoomDTO.setId(c.getId());
				divisionRoomDTO.setRoomName(c.getRoomName());
				divisionRoomDTO.setNumberOfStaff(c.getNumberOfStaff());
				divisionRoomDTOs.add(divisionRoomDTO);
			}
			Map<String, Object> listAllRoom = new HashMap<String, Object>();
			listAllRoom.put("roomDTOs", divisionRoomDTOs);
			return new ModelAndView("viewRoom", "listAllRoom",listAllRoom);
		}
		return new ModelAndView("viewRoom","",null);
	}
}
